package com.duleme.service.impl;

import com.duleme.common.EncryptUtil.MD5EncryptUtils;
import com.duleme.common.UserInfoUtils;
import com.duleme.dao.ShopDao;
import com.duleme.dao.UserDao;
import com.duleme.dao.UserInfoDao;
import com.duleme.entity.Shop;
import com.duleme.entity.User;
import com.duleme.entity.UserInfo;
import com.duleme.service.UserService;

public class UserServiceImpl implements UserService {
    UserDao dao=new UserDao();
    UserInfoDao userInfoDao = new UserInfoDao();
    ShopDao shopDao = new ShopDao();

    public boolean register(User user) {
        System.out.println(dao.checkAccountValid(user.getUserAccount()));
        if (dao.checkAccountValid(user.getUserAccount())){
            System.out.println(user.getUserName());
            dao.insert(user);
            return true;
        }else {
            return false;
        }
    }

    public User login(String account, String pwd) {
        return dao.findUserByAccAndPwd(account,pwd);
    }

    public UserServiceImpl() {
        super();
    }

    @Override
    public Boolean userCerification(UserInfo userInfo) {
        if (userInfo.getUserId()>100000&&userInfo.getId()!=null&&userInfo.getName()!=null){
            return userInfoDao.updateIdAndName(userInfo.getUserId(),userInfo.getId(),userInfo.getName());
        }
        return false;
    }

    @Override
    public Boolean initUserInfo(int userID) {
        if (userID>=100000){
            if (userInfoDao.findByUserId(userID) ==null) {
                return userInfoDao.insert(userID);
            }else {
                return false;
            }
        }else {
            System.out.println("init failed");
            return false;
        }
    }

    @Override
    public Boolean addUserInfo(UserInfo userInfo) {
/*        Boolean flagBirthday = false;
        Boolean flagSex = false;
        Boolean flagPhoneNum = false;
        Boolean flagEmail = false;
        UserInfoDao userInfoDao = new UserInfoDao();
        if (userInfo.getUserId()>10000){
            if (userInfo.getBirthday()!=null){
                flagBirthday = userInfoDao.updateInfo(userInfo.getUserId(), UserInfoUtils.Birthday,String.valueOf(userInfo.getBirthday().getTime()));
            }
            if (userInfo.getEmailAddress()!=null){
                flagEmail = userInfoDao.updateInfo(userInfo.getUserId(),UserInfoUtils.EmailAddress,userInfo.getEmailAddress());
            }
            if (userInfo.getSex()>=0){
                flagSex = userInfoDao.updateInfo(userInfo.getUserId(),UserInfoUtils.Sex,String.valueOf(userInfo.getSex()));
            }
            if (userInfo.getPhoneNumber()!=null){
                flagEmail = userInfoDao.updateInfo(userInfo.getUserId(),UserInfoUtils.PhoneNumber,userInfo.getPhoneNumber());
            }
            return flagBirthday||flagSex||flagPhoneNum||flagEmail;
        }else {
            return false;
        }*/
        return userInfoDao.updateUserInfo(userInfo);
    }

    public UserInfo getUserInfo(String account, String password) {
        UserService userService = new UserServiceImpl();
        User user = userService.login(account,password);
        if (user == null){
            System.out.println("user不存在");
            return null;
        }else {
            int userID =user.getUserId();
            if (userID>=100000){
                if (userInfoDao.findByUserId(userID) ==null) {
                    System.out.println("user存在，但是userInfo不存在");
                    if(userInfoDao.insert(userID)){
                        UserInfo userInfo = new UserInfo();
                        userInfo.setUserId(userID);
                        System.out.println("插入了");
                        return userInfo;
                    }
                    else {
                        System.out.println("没插入了");
                        return null;

                    }
                }else {
                    return userInfoDao.findByUserId(userID);
                }
            }else {
                return null;
            }
        }

    }

    @Override
    public Shop getShop(int userID) {
        int shopID = shopDao.findShopIdByOwnerId(userID);
        if (shopID==-1){
            return null;
        }else {
            return shopDao.findShopById(shopID);
        }
    }

    @Override
    public Boolean alterPassword(String userAccount,String password) {
        if (userAccount!=null&&password!=null){
            return dao.alterPassword(userAccount, MD5EncryptUtils.md5Encrypt((password)));
        }
        return false;
    }
}
